<template>
  <div class="HmLogin">
    <!-- 导航栏 -->
    <van-nav-bar title="注册" />
    <!-- 下面的input框 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册账号</van-button
        >
      </div>
    </van-form>
    <router-link class="link" to="/HmLogin">已有账号？返回登录</router-link>
  </div>
</template>

<script>
import { register } from '@/api/user'
import { getToken } from '@/utils/storage'
export default {
  name: 'HmLogin',
  data() {
    return {
      username: 'harry17',
      password: '123456'
    }
  },
  methods: {
    async onSubmit(values) {
      // 往后台发送注册请求了
      const data = await register(values)
      console.log(data)

      getToken(data.token)
      this.$toast.success('注册成功')
      this.$router.push('/HmLogin')
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
